package org.example.JUC;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ExchangerTest {

    private static final Exchanger<Stock> exchanger = new Exchanger<>();

    public static void main(String[] args) {

        ExecutorService es = Executors.newFixedThreadPool(5);
        List<Stock> stocks = new ArrayList<>();
        stocks.add(new Stock("000001", "平安银行", "19.70"));
        stocks.add(new Stock("000002", "万科A", "31.61"));
        stocks.add(new Stock("002714", "牧原股份", "77.18"));
        stocks.add(new Stock("002456", "欧菲光", "15.37"));
        stocks.add(new Stock("000977", "浪潮信息", "28.08"));
        stocks.add(new Stock("002555", "三七互娱", "24.98"));
        for (int i = 0; i < 6; i++) {
            Stock stock = stocks.get(i);
            Trader trader = new Trader("散户" + i, stock, exchanger);
            System.out.println("[" + trader.getName() + "]持仓" + stock);
            es.submit(trader);
        }

        es.shutdown();
    }

    static class Trader implements Runnable {

        private String name;

        private Stock stock;

        private Exchanger<Stock> exchanger;

        public Trader(String name, Stock stock, Exchanger<Stock> exchanger) {
            this.name = name;
            this.stock = stock;
            this.exchanger = exchanger;
        }

        @Override
        public void run() {

            try {
                TimeUnit.MILLISECONDS.sleep(500);
                System.out.println("[" + name + "]卖出股票" + stock);
                Stock exchangeStock = exchanger.exchange(stock);
                System.out.println("[" + name + "]买入股票" + exchangeStock);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Stock getStock() {
            return stock;
        }

        public void setStock(Stock stock) {
            this.stock = stock;
        }

        public Exchanger<Stock> getExchanger() {
            return exchanger;
        }

        public void setExchanger(Exchanger<Stock> exchanger) {
            this.exchanger = exchanger;
        }
    }

    static class Stock {

        private String code;

        private String name;

        private String price;

        public Stock(String code, String name, String price) {
            this.code = code;
            this.name = name;
            this.price = price;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        @Override
        public String toString() {
            return "Stock{" +
                    "code='" + code + '\'' +
                    ", name='" + name + '\'' +
                    ", price='" + price + '\'' +
                    '}';
        }
    }
}

